//
//  UIText+Extension.swift
//  UIKitExtentions
//
//  Created by Alean on 2022/12/22.
//

import Foundation
//MARK:--- UITextField ----------
public extension WDExtension where Base: UITextField {
    @discardableResult
    func placeholder(_ p: String? = nil) -> WDExtension {
        base.placeholder = p
        return self
    }
    @discardableResult
    func placeholder(_ attributed: NSAttributedString? = nil) -> WDExtension {
        base.attributedPlaceholder = attributed
        return self
    }
    @discardableResult
    func border(_ style:UITextField.BorderStyle) -> WDExtension {
        base.borderStyle = style
        return self
    }
    @discardableResult
    func text(default attributes:[NSAttributedString.Key : Any]) -> WDExtension {
        base.defaultTextAttributes = attributes
        return self
    }
    @discardableResult
    func clear(onBeginEditing b:Bool) -> WDExtension {
        base.clearsOnBeginEditing = b
        return self
    }
    @discardableResult
    func clear(buttonMode b:UITextField.ViewMode) -> WDExtension {
        base.clearButtonMode = b
        return self
    }
    @discardableResult
    func adjusts(_ fontSizeToFitWidth:Bool) -> WDExtension {
        base.adjustsFontSizeToFitWidth = fontSizeToFitWidth
        return self
    }
    @discardableResult
    func minimumFont(_ size:CGFloat) -> WDExtension {
        base.minimumFontSize = size
        return self
    }
    @discardableResult
    func delegate(_ d:UITextFieldDelegate? = nil) -> WDExtension {
        base.delegate = d
        return self
    }
    @discardableResult
    func background(_ img:UIImage? = nil) -> WDExtension {
        base.background = img
        return self
    }
    @discardableResult
    func disabledBackground(_ img:UIImage? = nil) -> WDExtension {
        base.disabledBackground = img
        return self
    }
    @discardableResult
    func allows(editingTextAttributes b:Bool) -> WDExtension {
        base.allowsEditingTextAttributes = b
        return self
    }
    @discardableResult
    func typing(attributes b:[NSAttributedString.Key : Any]) -> WDExtension {
        base.typingAttributes = b
        return self
    }
    
    @discardableResult
    func left(view v:UIView) -> WDExtension {
        base.leftView = v
        return self
    }
    @discardableResult
    func left(viewMode v:UITextField.ViewMode) -> WDExtension {
        base.leftViewMode = v
        return self
    }
    @discardableResult
    func right(view v:UIView) -> WDExtension {
        base.rightView = v
        return self
    }
    @discardableResult
    func right(viewMode v:UITextField.ViewMode) -> WDExtension {
        base.rightViewMode = v
        return self
    }
    
    @discardableResult
    func returnKeyType(_ a:UIReturnKeyType) -> WDExtension {
        base.returnKeyType = a
        return self
    }
    
    @discardableResult
    func keyboardType(_ type:UIKeyboardType) -> WDExtension {
        base.keyboardType = type
        return self
    }
}


//MARK:--- UITextView ----------
public extension WDExtension where Base: UITextView {
    
    @discardableResult
    func delegate(_ d:UITextViewDelegate? = nil) -> WDExtension {
        base.delegate = d
        return self
    }
    
    @discardableResult
    func allows(editingTextAttributes b:Bool) -> WDExtension {
        base.allowsEditingTextAttributes = b
        return self
    }
    @discardableResult
    func typing(attributes b:[NSAttributedString.Key : Any]) -> WDExtension {
        base.typingAttributes = b
        return self
    }
    
    @discardableResult
    func isEditable(_ e: Bool) -> WDExtension {
        base.isEditable = e
        return self
    }
    
    @discardableResult
    func isSelectable(_ s: Bool) -> WDExtension {
        base.isSelectable = s
        return self
    }
    
    @discardableResult
    func text(containerInset c: UIEdgeInsets) -> WDExtension {
        base.textContainerInset = c
        return self
    }
    
    @discardableResult
    func data(detectorTypes d: UIDataDetectorTypes) -> WDExtension {
        base.dataDetectorTypes = d
        return self
    }
    
    @discardableResult
    func returnKeyType(_ a:UIReturnKeyType) -> WDExtension {
        base.returnKeyType = a
        return self
    }
    
    @discardableResult
    func keyboardType(_ type:UIKeyboardType) -> WDExtension {
        base.keyboardType = type
        return self
    }
}

public protocol UITextProtocol: NSObjectProtocol {
    var txt: String {set get}
}

extension UITextField: UITextProtocol {
    public var txt: String {
        get {
            return self.text ?? ""
        }
        set {
            self.text = newValue
        }
    }
}

extension UITextView: UITextProtocol {
    public var txt: String {
        get {
            return self.text
        }
        set {
            self.text = newValue
        }
    }
}
